package controller.Operation;

import model.Book;
import model.NormalUser;
import util.BookDao;
import util.NormalUserDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.LinkedList;
import java.util.List;

/**
 * @Question Search
 * @Date 2022/12/18 19:51
 * @Solution
 */
public class Search {
    public static void bookSearch(JPanel Container, DefaultTableModel bookModel, JTable bookTable, JTextField bookID,
                                  JTextField bookNAME, JTextField bookAUTHOR, JTextField bookPRESS ,JRadioButton canBor) {
        String bookName=bookNAME.getText();
        String bookAuthor=bookAUTHOR.getText();
        String bookPress=bookPRESS.getText();
        boolean state=canBor.isSelected();
        BookDao bookDao = new BookDao();
        List<Book> list = bookDao.getAll();//拿到所有书
        //筛选出符合条件的
        if(!bookID.getText().equals("")) {
            int bookId=Integer.parseInt(bookID.getText());
            List<Book> needDel1=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                Book book=list.get(i);
                if(book.getBookId()!=bookId) {
                    needDel1.add(book);
                }
            }
            for(Book book:needDel1) {
                list.remove(book);
            }
        }
        if(!bookName.equals("")) {
            List<Book> needDel2=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                Book book=list.get(i);
                if(!book.getBookName().equals(bookName)) {
                    needDel2.add(book);
                }
            }
            for(Book book:needDel2) {
                list.remove(book);
            }
        }
        if(!bookAuthor.equals("")) {
            List<Book> needDel3=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                Book book=list.get(i);
                if(!book.getBookAuthor().equals(bookAuthor)) {
                    needDel3.add(book);
                }
            }
            for(Book book:needDel3) {
                list.remove(book);
            }
        }
        if(!bookPress.equals("")) {
            List<Book> needDel4=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                Book book=list.get(i);
                if(!book.getBookPre().equals(bookPress)) {
                    needDel4.add(book);
                }
            }
            for(Book book:needDel4) {
                list.remove(book);
            }
        }
        if(state) {
            List<Book> needDel5=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                Book book=list.get(i);
                if(!book.isState()==state) {
                    needDel5.add(book);
                }
            }
            for(Book book:needDel5) {
                list.remove(book);
            }
        }
        bookModel.setRowCount(0);
        for(Book book:list) {
            if(book.isState()) {
                Object[] info = {book.getBookId(), book.getBookName(),
                        book.getBookAuthor(), book.getBookPre(), "可借"};
                bookModel.addRow(info);
            } else {
                Object[] info = {book.getBookId(), book.getBookName(),
                        book.getBookAuthor(), book.getBookPre(), "不可借"};
                bookModel.addRow(info);
            }
        }
    }
    public static void userSearch(JPanel Container,DefaultTableModel userModel, JTable userTable,JTextField userIdText,
                                  JTextField userNameText,JTextField userPasswordText) {
        String userName=userNameText.getText();
        String userPassword=userPasswordText.getText();
        NormalUserDao normalUserDao = new NormalUserDao();
        List<NormalUser> list = normalUserDao.getAll();//拿到所有书
        //筛选出符合条件的
        if(!userIdText.getText().equals("")) {
            int userId=Integer.parseInt(userIdText.getText());
            List<NormalUser> needDel1=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                NormalUser user=list.get(i);
                if(user.getId()!=userId) {
                    needDel1.add(user);
                }
            }
            for(NormalUser user:needDel1) {
                list.remove(user);
            }
        }
        if(!userName.equals("")) {
            List<NormalUser> needDel2=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                NormalUser user=list.get(i);
                if(!user.getUsername().equals(userName)) {
                    needDel2.add(user);
                }
            }
            for(NormalUser user:needDel2) {
                list.remove(user);
            }
        }
        if(!userPassword.equals("")) {
            List<NormalUser> needDel3=new LinkedList<>();
            for(int i=0;i<list.size();i++) {
                NormalUser user=list.get(i);
                if(!user.getPassword().equals(userPassword)) {
                    needDel3.add(user);
                }
            }
            for(NormalUser user:needDel3) {
                list.remove(user);
            }
        }
        userModel.setRowCount(0);
        for(NormalUser user:list) {
            Object[] info = {user.getId(),user.getUsername(), user.getPassword()};
            userModel.addRow(info);
        }
    }
}
